package com.pkk.cloud.support.oauth2.client.feign.configuration;

import feign.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;

/**
 * @Description: 开启客户端Feign调用
 * @Author: peikunkun
 * @Date: 2020/3/21 0021 下午 2:07
 */
public class Oauth2ClientFeignConfiguration {

  // feign的OAuth2ClientContext
  private OAuth2ClientContext feignOAuth2ClientContext = new DefaultOAuth2ClientContext();

  //客户端的认证凭证信息
  @Autowired
  private ClientCredentialsResourceDetails clientCredentialsResourceDetails;

  /**
   * @Description: 开启请求oauth2拦截器
   * <p>
   * 会讲请求头的token设置为服务之间的请求token
   * @return: feign.RequestInterceptor
   * @Author: peikunkun
   * @Date: 2020/3/21 0021 下午 2:07
   */
  @Bean
  public RequestInterceptor oauth2FeignRequestInterceptor() {
    return new OAuth2FeignRequestInterceptor(feignOAuth2ClientContext, clientCredentialsResourceDetails);
  }

  @Bean
  public OAuth2RestTemplate clientCredentialsRestTemplate() {
    return new OAuth2RestTemplate(clientCredentialsResourceDetails);
  }
}
